package com.backstage.service.Impl;

import com.backstage.mapper.ArticleMapper;
import com.backstage.mapper.SysUserMapper;
import com.backstage.pojo.dto.Message;
import com.backstage.pojo.entity.CArticle;
import com.backstage.pojo.entity.SysRolePermission;
import com.backstage.pojo.entity.SysUser;
import com.backstage.pojo.vo.ArticleVo;
import com.backstage.pojo.vo.SysUserVo;
import com.backstage.service.ArticleService;
import com.backstage.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    SysUserMapper userMapper;

    @Autowired
    SysUtilServiceImpl sysUtilService;
    @Override
    public Message<List<ArticleVo>> getAllArticle(ArticleVo articleVo) {
        Page<CArticle> page=new Page<>();
        page.setCurrent(articleVo.getPage());
        page.setSize(articleVo.getRows());
        QueryWrapper<CArticle> wrapper=new QueryWrapper();
        if(articleVo.getTypeId()!=null){
          wrapper.eq("type_id",articleVo.getTypeId());
        }
        if(articleVo.getKeywords()!=null){
            wrapper.like("keywords",articleVo.getKeywords());
        }
        if(articleVo.getCity()!=null){
            wrapper.eq("city",articleVo.getCity());
        }
        if(articleVo.getUserNum()!=null){
            wrapper.eq("user_num",articleVo.getUserNum());
        }
        IPage<CArticle> page1 = articleMapper.selectPage(page, wrapper);
        List<ArticleVo> result=new ArrayList<>();
        for (int i = 0; i < page1.getRecords().size(); i++) {
            CArticle cArticle = page1.getRecords().get(i);
            ArticleVo articleVo1 = PojoUtil.copyObject(cArticle, ArticleVo.class);
            articleVo1.setPage(articleVo.getPage());
            articleVo1.setRows(articleVo.getPage());
            result.add(articleVo1);
        }
            return Message.successMessage(page1.getTotal(),result);
    }
    @Override
    public Message<List<ArticleVo>> getState(ArticleVo articleVo) {
        Page<CArticle> page=new Page<>();
        page.setCurrent(articleVo.getPage());
        page.setSize(articleVo.getRows());
        QueryWrapper wrapper=new QueryWrapper();
       wrapper.eq("state",2);
        IPage<CArticle> page1 = articleMapper.selectPage(page, wrapper);
        List<ArticleVo> result=new ArrayList<>();
        for (int i = 0; i < page1.getRecords().size(); i++) {
            CArticle cArticle = page1.getRecords().get(i);
            ArticleVo articleVo1 = PojoUtil.copyObject(cArticle, ArticleVo.class);
            articleVo1.setPage(articleVo.getPage());
            articleVo1.setRows(articleVo.getPage());
            result.add(articleVo1);
        }
        return Message.successMessage(page1.getTotal(),result);
    }
    @Override
    public Message<ArticleVo> getArticle(ArticleVo articleVo) {
    QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",articleVo.getId());
        wrapper.eq("state",2);
        CArticle cArticle = articleMapper.selectOne(wrapper);
        ArticleVo articleVo1 = PojoUtil.copyObject(cArticle, ArticleVo.class);
        return Message.successMessage(1L,articleVo1);
    }

    @Override
    public Message<String> deleteArticleById(Long id) {
        int cArticle = articleMapper.deleteById(id);
        if(cArticle>0) {
            return Message.successMessage(1L, "删除成功");
        }
        return Message.errorMessage("0000","删除失败");
    }

    @Override
    public Message<String> addArticle(ArticleVo article) {
        CArticle cArticle = PojoUtil.copyObject(article, CArticle.class);
        int insert = articleMapper.insert(cArticle);
        if(insert>0){
            return Message.successMessage(1L, "添加成功");
        }
        return Message.errorMessage("0000","添加失败");
    }

    @Override
    public Message<String> updateArticle(ArticleVo article) {
        CArticle cArticle = PojoUtil.copyObject(article, CArticle.class);
        int update = articleMapper.updateById(cArticle);
        if(update>0){
            return Message.successMessage(1L, "修改成功");
        }
        return Message.errorMessage("0000","修改失败");
    }
    public SysUser getUser(Long id){
        SysUser sysUser = userMapper.selectById(id);
        return sysUser;
    }
}
